// ignore_for_file: always_specify_types

import 'dart:collection';
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:medical_pro_flutter/api/Api.dart';
import 'package:medical_pro_flutter/components/dialog/LoadingDialog.dart';
import 'package:medical_pro_flutter/components/dialog/SuccessDialog.dart';
import 'package:medical_pro_flutter/components/illcase/ShowCasePage.dart';
import 'package:medical_pro_flutter/entity/IllCase.dart';
import 'package:medical_pro_flutter/entity/User.dart';

class MyCaseList extends StatefulWidget {
  MyCaseList({Key? key}) : super(key: key);

  @override
  _MyCaseListState createState() => _MyCaseListState();
}

class _MyCaseListState extends State<MyCaseList> {
  // List caseInfo = [
  //   //测试假数据
  //   {'patient_name': '1212', 'date': '2121-12-12'}
  // ];
  List caseInfo = [];

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(child: _getCaseList(), onRefresh: _submitSearch);
  }

  Future _submitSearch() async {
    String url = API.BASE_URL + API.ILL_Case + 'uploder/' + User.job_num;

    print('发出请求：$url');
    Response response;

    try {
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return LoadingDialog(text: '加载中...');
          });
      var dio = Dio();
      response = await dio.get(url);
      dio.close();

      if (response.statusCode == 200) {
        print(response.data);
        // List<IllCase> va = json.decode(response.data);
        List va = List<dynamic>.from(response.data);
        setState(() {
          caseInfo = va;
        });

        // print(caseInfo[0]['case_id']);
        // print('总共${caseInfo.length}个病例');

        Navigator.pop(context);
        if (caseInfo.isEmpty) {
          Fluttertoast.showToast(
            msg: '没有数据了',
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            timeInSecForIosWeb: 3,
            backgroundColor: Colors.red,
            textColor: Colors.white,
            fontSize: 16.0,
          );
        }
      }
    } catch (e) {
      print(e);
    }
  }

  _getCaseList() {
    print(caseInfo.toString());
    if (caseInfo.length != 0) {
      return ListView.builder(
          // scrollDirection: Axis.vertical,
          // shrinkWrap: true,
          // physics: AlwaysScrollableScrollPhysics(),
          itemCount: caseInfo.length,
          itemBuilder: (BuildContext context, int index) {
            return _itemBuilder(context, index);
          });
    } else {
      // 加载菊花
      return ListView.builder(
          itemCount: 1,
          itemBuilder: (BuildContext context, int index) {
            return const ListTile(
              title: Text('下拉加载数据'),
              // subtitle: Text('您还未上传过病例'),
            );
          });
    }
  }

  Widget _itemBuilder(BuildContext context, int index) {
    return ListTile(
        title: Text('患者姓名：${caseInfo[index]['patient_name']}'),
        subtitle: Text('诊断日期：${caseInfo[index]['date']}'),
        onTap: () {
          print('点击了$index条' + caseInfo[index].runtimeType.toString());

          Navigator.pushNamed(context, '/show_case_page',
              arguments: caseInfo[index]);
        }
        // () => ShowCasePage(caseInfo[index])
        );
  }
}
